package com.mujiwulian.vue.controller;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


import java.util.ArrayList;
import java.util.List;


public class TestController {
//    @Autowired
//    private RestTemplate restTemplate;

    public static void tologin(){
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();      //map里面是请求体的内容
        map.add("username", "admin");
        map.add("password","admin888");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
//        ResponseEntity<String> response = restTemplate.postForEntity("http://hj.mujiwulian.net/web/index.php?r=admin", request, String.class);       //地址替换为自己的
        ResponseEntity<String> response = restTemplate.getForEntity("http://hj.mujiwulian.net",String.class, request);
             //地址替换为自己的
        Document doc = Jsoup.parse(response.getBody());
        Element element = doc.select("head").select("script").get(0);
        Element elementCaptcha_code=doc.select("img[class=refresh-captcha]").get(0);
        System.out.println("http://hj.mujiwulian.net"+elementCaptcha_code.attributes().get("src"));
        String temp=element.toString();
        String _csrf=temp.substring(temp.indexOf("\"")+1,temp.lastIndexOf("\""));
        HttpHeaders httpHeaders=response.getHeaders();
        System.out.println(httpHeaders);
        String _csrfCookietemp=httpHeaders.get("Set-Cookie").get(1);
        String DBSESSIONIDtemp=httpHeaders.get("Set-Cookie").get(0);
        String _csrfCookie=_csrfCookietemp.substring(_csrfCookietemp.indexOf("=")+1,_csrfCookietemp.lastIndexOf("; path=/; httponly"));
        String DBSESSIONID=DBSESSIONIDtemp.substring(DBSESSIONIDtemp.indexOf("=")+1,DBSESSIONIDtemp.lastIndexOf("; path=/; HttpOnly"));
        System.out.println(_csrfCookietemp);
        System.out.println(DBSESSIONIDtemp);
        login(_csrf,_csrfCookie,DBSESSIONID);
    }
    public static void login(String _csrf,String __csrfCookie,String DBSESSIONID){
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        List<String> cookies =new ArrayList<String>();
        /* 登录获取Cookie 这里是直接给Cookie，可使用下方的login方法拿到Cookie给入*/
        cookies.add(__csrfCookie);       //在 header 中存入cookies
        cookies.add(DBSESSIONID);
        headers.put(HttpHeaders.COOKIE,cookies);        //将cookie存入头部
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();      //请求体给予内容
        map.add("username", "admin");
        map.add("password","admin888");
        map.add("_csrf",_csrf);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity("https://hj.mujiwulian.net/web/index.php?r=admin/passport/login", request, String.class);
//        toIndex(__csrfCookie);


        System.out.println(response.getHeaders());
//        System.out.println(response);
    }

    public static void toIndex(String __csrfCookie){
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        List<String> cookies =new ArrayList<String>();
        /* 登录获取Cookie 这里是直接给Cookie，可使用下方的login方法拿到Cookie给入*/
        cookies.add(__csrfCookie);       //在 header 中存入cookies
        headers.put(HttpHeaders.COOKIE,cookies);        //将cookie存入头部
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();      //请求体给予内容
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.getForEntity("http://hj.mujiwulian.net/web/index.php?r=mch/store/index", String.class,request);
        System.out.println(response);
    }
    public static void main(String[] args) {
        tologin();
    }
}
